﻿using FPSO.Models.Constant;
using FPSO.Models.Domain;
using FPSO.Models.TagMapping;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static FPSO.Models.Constant.TankConstant;

namespace FPSO.HMI.Views.Dialog
{
	/// <summary>
	/// TaskTankCheckDialog.xaml 的交互逻辑
	/// </summary>
	public partial class TaskTankCheckDialog : Window
	{
		public ObservableCollection<DeviceDetail> _devices = new ObservableCollection<DeviceDetail>();
		public class DeviceDetail
		{
			public DeviceDetail(string deviceName, string deviceType, string planState, string nowState, string keyType, string opcId)
			{
				DeviceName = deviceName;
				DeviceType = deviceType;
				PlanState = planState;
				NowState = nowState;
				KeyType = keyType;
				OpcId = opcId;
			}

			public string DeviceName { get; set; }
			public string DeviceType { get; set; }
			public string PlanState { get; set; }
			public string NowState { get; set; }
			public string KeyType { get; set; }
			public string OpcId { get; set; }


		}

		public TaskTankCheckDialog(TankTask tankTask)
		{
			InitializeComponent();

			if (tankTask.TaskType == TaskTypeConstant.LOAD_ID)
			{
				getLoadDevice(tankTask);
			}

			if (tankTask.TaskType == TaskTypeConstant.UNLOAD_ID)
			{
				getUnLoadDevice(tankTask);
			}

			if (tankTask.TaskType == TaskTypeConstant.SWAP_ID)
			{
				getSwapDevice(tankTask);
			}

			taskDeviceList.ItemsSource = _devices;
		}

		private void getLoadDevice(TankTask tankTask)
		{
			label_tasktype.Content = "装载";
			label_from_tank.Content = "外部舱";
			label_to_tank.Content = tankTask.TankId;
			loadTaskRelation("", tankTask.TankId, TaskTypeConstant.LOAD_ID);
		}

		private void getUnLoadDevice(TankTask tankTask)
		{
			label_tasktype.Content = "卸载";
			label_from_tank.Content = tankTask.TankId;
			label_to_tank.Content = "外部舱";
			loadTaskRelation(tankTask.TankId, "", TaskTypeConstant.UNLOAD_ID);
		}

		private void getSwapDevice(TankTask tankTask)
		{
			label_tasktype.Content = "导舱";
			label_from_tank.Content = tankTask.TankId;
			label_to_tank.Content = tankTask.SwapTargetTank1;
			loadTaskRelation(tankTask.TankId, tankTask.SwapTargetTank1, TaskTypeConstant.SWAP_ID);
		}

		private void loadTaskRelation(string fromTank, string toTank, int taskType)
		{
			List<ConstantTaskDevice> deviceList = TankConstant.GetTaskRelationValve(fromTank, toTank, taskType);
			string deviceId = "";
			foreach (ConstantTaskDevice device in deviceList)
			{
				ConstantDeviceInfo deviceInfo = TankConstant.getDeviceInfo(device.DeviceId);

				List<ConstantTagInfo> tagInfos = deviceInfo.TagInfos;

				List<string> keyTagIds = device.TagIds;

				foreach (ConstantTagInfo tagInfo in tagInfos)
				{
					if (deviceId != device.DeviceId)
					{
						deviceId = device.DeviceId;
						DeviceDetail item = new DeviceDetail(deviceId, deviceInfo.getDeviceType(), device.getPlanState(tagInfo.TagID), "", keyTagIds.Contains(tagInfo.TagID) ? "是" : "", tagInfo.TagID);
						_devices.Add(item);
					}
					else
					{
						DeviceDetail item = new DeviceDetail("", "", device.getPlanState(tagInfo.TagID), "", keyTagIds.Contains(tagInfo.TagID) ? "是" : "", tagInfo.TagID);
						_devices.Add(item);
					}
				}
			}
		}

		private void Confirm_Click(object sender, RoutedEventArgs e)
		{
			//DEBUG 这里应该有设备检查的步骤
			this.DialogResult = true;
			this.Close();
		}

		private void Exit_Click(object sender, RoutedEventArgs e)
		{
			this.DialogResult = false;
			this.Close();
		}
	}
}